{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d34b521f",
   "metadata": {},
   "outputs": [],
   "source": [
    "from glob import glob\n",
    "from common_utils import read_jsonl\n",
    "import gzip\n",
    "import json\n",
    "import os\n",
    "from tqdm import tqdm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "fe8eae72",
   "metadata": {},
   "outputs": [],
   "source": [
    "para_paths = glob(\"/home/jimx/codes/TB_KB_Search/enwiki-20181220_paras/para_*.jsonl\")\n",
    "para_paths.sort()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2cdd0691",
   "metadata": {},
   "outputs": [],
   "source": [
    "datas = read_jsonl(para_paths[0])"
   ]
  },
  {
   "cell_type": "raw",
   "id": "a75cdccb",
   "metadata": {},
   "source": [
    "datas[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9f9d23a9",
   "metadata": {},
   "outputs": [],
   "source": [
    "para_id_to_txt = [{\"para_id\":item[\"para_id\"], \"para_text\":item[\"para_text\"]} for item in datas if item[\"sentence_in_doc\"]==0]"
   ]
  },
  {
   "cell_type": "raw",
   "id": "34f73660",
   "metadata": {},
   "source": [
    "para_id_to_txt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "984c5596",
   "metadata": {},
   "outputs": [],
   "source": [
    "with gzip.open(\"test.gz\",\"wb\")as f1:\n",
    "    text=json.dumps(para_id_to_txt, indent=4, ensure_ascii=False).encode(\"utf-8\")\n",
    "    f1.write(text)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "adb1a286",
   "metadata": {},
   "source": [
    "## 封装"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a9f2594d",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 283/283 [08:26<00:00,  1.79s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "13285679\n",
      "5385455\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "para_paths = glob(\"/home/jimx/codes/TB_KB_Search/enwiki-20181220_paras/para_*.jsonl\")\n",
    "para_paths.sort()\n",
    "para_id_to_txt = []\n",
    "para_count = 0\n",
    "wiki_count = 0\n",
    "for path in tqdm(para_paths):\n",
    "    datas = read_jsonl(path)\n",
    "    \n",
    "    para_count += len(datas)\n",
    "    \n",
    "    _tmp1 = [1 for item in datas if item[\"sentence_in_doc\"]==0]\n",
    "    wiki_count += sum(_tmp1)\n",
    "\n",
    "print(para_count)\n",
    "print(wiki_count)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8ffd9d54",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 283/283 [09:16<00:00,  1.97s/it]\n"
     ]
    }
   ],
   "source": [
    "para_paths = glob(\"/home/jimx/codes/TB_KB_Search/enwiki-20181220_paras/para_*.jsonl\")\n",
    "para_paths.sort()\n",
    "para_id_to_txt = []\n",
    "for path in tqdm(para_paths):\n",
    "    datas = read_jsonl(path)\n",
    "    _id_to_txt = [{\"id\":item[\"para_id\"], \"text\":item[\"para_text\"]} for item in datas if item[\"sentence_in_doc\"]==0]\n",
    "    para_id_to_txt.extend(_id_to_txt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b1fe2d5e",
   "metadata": {},
   "outputs": [],
   "source": [
    "with gzip.open('para_id_to_txt.gz', 'rb') as f:\n",
    "    para_id_to_txt = f.read().decode(\"utf-8\")\n",
    "    para_id_to_txt = json.loads(para_id_to_txt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "24fe2fd6",
   "metadata": {},
   "outputs": [],
   "source": [
    "len()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "b568a75b",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "101it [04:43,  2.80s/it]\n"
     ]
    }
   ],
   "source": [
    "# 存为100个小文件\n",
    "os.makedirs(\"wiki_para_id_to_txt\", exist_ok=True)\n",
    "step = int(len(para_id_to_txt) / 100)\n",
    "for part, start in tqdm(enumerate(range(0, len(para_id_to_txt), step))):\n",
    "    tmp = para_id_to_txt[start: min(start+step, len(para_id_to_txt))]\n",
    "    with gzip.open(f\"wiki_para_id_to_txt/para_id_to_txt_{part:03d}.gz\", \"wb\")as f1:\n",
    "        text=json.dumps(tmp, ensure_ascii=False).encode(\"utf-8\")\n",
    "        f1.write(text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5cc30a47",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "376e01dd",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
